var arr = [1, 3, 4, 7, 9];

//利用splice实现unShift

Array.prototype.myUnshift = function () {
    var pos = 0;
    for (var i = 0; i < arguments.length; i++) {
        this.splice(pos, 0, arguments[i]);
        pos++;
    }
    return this.length
}

// 去重
Array.prototype.unique = function () {
    var count = 0;
    for (var j = 0; j < this.length; j++) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == this[j]) {
                count++;
                if (count > 1) {
                    // 删除元素为i的元素
                    this.splice(i, 1)
                    count--;
                    i--;
                }
            }
        }
        count = 0;
    }
}

var len = arr.myUnshift(4, 5, 6, 7);
console.log(arr, len);

// 根据字节数排序

//1.计算做字节数
function comcputeByte(str) {
    let count = 0;
    for (let index = 0; index < str.length; index++) {
        let pos = str.charCodeAt(index);
        count += (pos > 255) ? 2 : 1;

    }
    return count

}

var brr = ['我爱你', 'OK', 'Hello', '你说WHAT', '可以']

// 2 自定义排序规则 <0 a在前
brr.sort(function (a, b) {
    return comcputeByte(a) - comcputeByte(b)
});
console.log(brr);

//封装自己的typeof 函数
function myTypeOf(val) {
    var type = typeof (val);
    var toStr = Object.prototype.toString;
    var res = {
        '[object array]': 'array',
        '[object Object]': 'Object',
        '[object Number]': 'Number',
        '[object String]': 'String',
        '[object Boolean]': 'Boolean'
    }
    if (val == null) {
        return 'NULL'
    } else if (type == "object") {
        // 用call调用val.toString()
        var ret = toStr.call(val)
        //等价
        ret = val.toString()
        return res[ret]
    } else {
        return type;
    }
}
console.log(myTypeOf(function () { }));
// 原数组去重
var testArr = [1, 1,2,12,1,2,3,2,3]
testArr.unique();
console.log(testArr)